import md5 from './md5.js';

const apis = {
    code2session: "/api/wechat/miniprogram/code2session",
    decryptUserInfo: "/api/wechat/miniprogram/decryptUserInfo",

    //user apis
    userInfo: "/api/user",
    myInfo: "/api/user/me",
    userSave: "/api/user/save",
    getMyCommentArticles: "/api/article/miniprogram/getMyCommentArticles",
    getMyLikeArticles: "/api/article/miniprogram/getMyLikeArticles",
    getMyPraisePosts: "/api/article/miniprogram/getMyPraisePosts",
    getNewComments: "/api/article/miniprogram/getNewComments",
    getMySubPost: "/api/article/miniprogram/getMySubPost",
    likeArticle: "/api/article/miniprogram/like",
    getMySubscribes: "/api/category/miniprogram/getMySubscribes",
    toSubscribe: "/api/category/miniprogram/toSubscribe",

    //article apis
    articleInfo: "/api/article/miniprogram",
    articleList: "/api/article/miniprogram/list",
    articleRelevantList: "/api/article/miniprogram/relevantList",
    articlePagination: "/api/article/miniprogram/paginate",
    articleCategoryInfo: "/api/category/miniprogram",
    articleCategories: "/api/article/miniprogram/categories",
    articleSave: "/api/article/miniprogram/save",

    //comment
    commentPaginate:"/api/article/miniprogram/commentPaginate",
    getArticleLikes:"/api/article/miniprogram/getArticleLikes",
    getWinxinQrcodeImg:"/api/article/miniprogram/getWinxinQrcodeImg",
    postComment: "/api/article/miniprogram/postComment",

    // pay
    toUnifiedOrder:"/api/pay/miniprogram/toUnifiedOrder",

    //page apis
    pageInfo: "/api/page",
    pageList: "/api/page/list",

    //option apis
    optionInfo: "/api/option",

    //others
    html2wxml:"/commons/html2wxml",
}

const config = {
    // 地址
    host: "",
    app_id: "",
    app_secret: "",
    sessionId: "",
    jwt: "",
    downloadFileDomain : [
      { id: 1, domain: 'www.watch-life.net'}
    ],
    postImageUrl:'',
    //回复评论消息模版id
    replayTemplateId:'IiAVoBWP34u1uwt801rI_Crgen7Xl2lvAGP67ofJLo8',
    // 网站的logo图片
    logo: "../../images/logo-icon.png",
    minAppType:"0",
    indexNav:[
        { id: '1', name: '微慕', image: '../../images/nav-icon1.png', url: 'pages/index/index', redirecttype: 'miniapp', appid: 'wx074a216a7aabc30c', extraData: '' },
        { id: '2', name: '企业版', image: '../../images/nav-icon2.png', url: 'pages/index/index', redirecttype: 'miniapp', appid: 'wxc1771b619b83316b', extraData: '' },
        { id: '3', name: '旅游版', image: '../../images/nav-icon3.png', url: 'pages/index/index', redirecttype: 'miniapp', appid: 'wxa43aaa8416cdbd61', extraData: '' },
        { id: '4', name: '图片版', image: '../../images/nav-icon4.png', url: 'pages/index/index', redirecttype: 'miniapp', appid: 'wxe9440e3d24b04152', extraData: ''},
        { id: '5', name: '微店', image: '../../images/nav-icon5.png', url: 'pages/shelf/shelf', redirecttype: 'miniapp', appid: 'wx55ea6098e41af5c4', extraData: ''}
        ],
    zanImageURL: 'https://f.wsxxg.cn/attachment/20190610/fd5c535f75164c29844f6458544b4257.jpg',//微信鼓励的图片链接，用于个人小程序的赞赏
}

const init = conf => {
  config.host = conf.host;
  config.app_id = conf.app_id;
  config.app_secret = conf.app_secret;

  var jwt = wx.getStorageSync("jwt");
  if(jwt){config.jwt = jwt}
}


const getUrl = (api, paras) => {

  paras = Object.assign({
    appId: config.app_id,
    t: new Date().getTime()
  }, paras);

  //对数据进行签名
  var signString = sign(paras);

  //添加签名结果
  paras = Object.assign({
    sign: signString
  }, paras);

  //拼接URL地址
  var url = config.host + api + "?"
  var arr = Object.keys(paras);
  for (var i in arr) {
    url = url + (arr[i] + "=" + paras[arr[i]]) + "&";
  }

  //remove last '&'
  return url.substring(0, url.length - 1);
}

const createGetRequest = req => {
  //default is get
  return createRequest(req);
}

const createPostRequest = req => {
  return createRequest(Object.assign({ method: 'POST' }, req));
}

const createRequest = (req = {
  api,
  paras,
  method,
  header,
  data,
}) => {

  var url = getUrl(req.api, req.paras);

  var realRequest = {
    url: url,
    method: (req.method == null ? 'GET' : req.method),
    header: Object.assign({ "Jwt": config.jwt }, req.header),
    data: req.data,
  }

  const p = new Promise((resolve, reject) => {
    wx.request(Object.assign({
      success: function (res) {
        //注意：第一个字母大写
        if (res.header.Jwt){
          updateJwt(res.header.Jwt);
        }
        //jpress 请求成功
        if (res.data.state == "ok") {
          resolve(res.data);
        } else {
          reject(res.data);
        }
      },
      error: function (e) {
        reject({
          code: 99,
          message: '网络错误'
        });
      }
    }, realRequest))
  });

  return {
    send: () => p
  }
}

/**
 * 对 obj 进行签名，返回签名内容
 * 要保证和JPress签名算法一致
 */
const sign = obj => {

  var secret = config.app_secret;

  //生成key升序数组，与JPress后台保存一致
  var arr = Object.keys(obj);
  arr.sort();

  var str = '';
  for (var i in arr) {
    str += arr[i] + obj[arr[i]].toString();
  }

  return md5(str + secret);
}


const code2session = code => {

  createRequest({
    api: apis.code2session,
    paras: { code: code }
  })
  .send()
  .then(data => {
    config.sessionId = data.sessionId;
    return true;
  })
  .catch(data => {
    return false;
  })
}

const updateJwt = (value) => {
  config.jwt = value;
  wx.setStorage({
    key: 'jwt',
    data: value,
  })
}

const decryptUserInfo =  (data = {
  rawData, signature, encryptedData, iv
},callback) => {

 createPostRequest({
    api: apis.decryptUserInfo,
    data: Object.assign({ sessionId: config.sessionId }, data)
  })
  .send()
  .then(data => {
    updateJwt(data.token);
    if (callback) callback(true,data.user);
  })
  .catch(data => {
    if (callback) callback(false);
  })

}



///////////////////////option api start/////////////////////////////////

/**
 * 获取网站配置信息
 */
const getOption = key => {
  return createGetRequest({
    api: apis.optionInfo,
    paras: { key: key }
  }).send()
}


const getAppName = () => {
  return getOption('wechat_miniprogram_name')
}

const getCopyright = () => {
  return getOption('wechat_miniprogram_copyright')
}

const getSlides = () => {
  return getOption('wechat_miniprogram_slides')
}

const getEnableComment = () => {
  return getOption('article_comment_enable')
}

///////////////////////option api end/////////////////////////////////

///////////////////////user api start/////////////////////////////////

/**
 * 获取用户信息
 */
const getUser = id => {
  return createGetRequest({
    api: apis.userInfo,
    paras: { id: id }
  }).send()
}

/**
 * 获取登录用户信息（我的信息）
 */
const getMyInfo = () => {
  return createGetRequest({
    api: apis.myInfo
  }).send()
}

/**
 * 用户信息保存
 */
const doUserSave = userData => {
  return createPostRequest({
    api: apis.userSave,
    data: userData,
  }).send()
}


/**
 * 获取我评论的文章列表
 */
const getMyCommentArticles = (paras = {
    openId,
}) => {
    return createGetRequest({
        api: apis.getMyCommentArticles,
        paras: paras
    }).send()
}

/**
 * 获取我评论的文章列表
 */
const getMyLikeArticles = (paras = {
    openId,
    source,
    orderBy,
    page
}) => {
    return createGetRequest({
        api: apis.getMyLikeArticles,
        paras: paras
    }).send()
}

/**
 * 获取我赞赏的文章列表
 */
const getMyPraisePosts = (paras = {
    openId,
}) => {
    return createGetRequest({
        api: apis.getMyPraisePosts,
        paras: paras
    }).send()
}

/**
 * 获取我订阅的文章列表
 */
const getMySubPost = (paras = {
    openId,
}) => {
    return createGetRequest({
        api: apis.getMySubPost,
        paras: paras
    }).send()
}
/**
 * 获取最新评论的文章列表
 */
const getNewComments = () => {
    return createGetRequest({
        api: apis.getNewComments,
    }).send()
}

/**
 * 喜欢文章
 */
const likeArticle = (paras = {
    openId,
    articleId
}) => {
    return createGetRequest({
        api: apis.likeArticle,
        paras: paras
    }).send()
}

const getMySubscribes = (paras = {
    openId
}) => {
    return createGetRequest({
        api: apis.getMySubscribes,
        paras: paras
    }).send()
}

const toSubscribe = (paras = {
    openId,
    categoryId
}) => {
    return createGetRequest({
        api: apis.toSubscribe,
        paras: paras
    }).send()
}


///////////////////////user api end/////////////////////////////////


///////////////////////article api start/////////////////////////////////

/**
 * 获取文章信息
 */
const getArticle = id => {
  return createGetRequest({
    api: apis.articleInfo,
    paras: { id: id }
  }).send()
}

/**
 * 获取文章信息
 */
const getArticleBySlug = paras => {
    return createGetRequest({
        api: apis.articleInfo,
        paras: paras
    }).send()
}

/**
 * 获取文章列表
 */
const getArticleList = (paras = {
  flag,
  hasThumbnail,
  orderBy,
  count
}) => {
  return createGetRequest({
    api: apis.articleList,
    paras: paras
  }).send()
}


/**
 * 获取文章的相关文章
 */
const getArticleRelevantList = (paras = {articleId,count}) => {
  return createGetRequest({
    api: apis.articleRelevantList,
    paras: paras
  }).send()
}


/**
 * 分页获取文章内容
 */
const getArticlePage = (paras = {
  categoryId,
  orderBy,
  page,
}) => {
  return createGetRequest({
    api: apis.articlePagination,
    paras: paras
  }).send()
}

/**
 * 获取文章分类信息
 */
const getArticleCategory = (paras = {
    openId
}) => {
  return createGetRequest({
    api: apis.articleCategoryInfo,
    paras: paras
  }).send()
}

/**
 * 获取文章分类信息
 */
const getArticleCategories = type => {
  return createGetRequest({
    api: apis.articleCategories,
    paras: {type:type}
  }).send()
}

/**
 * 保存文章
 */
const doArticleSave = articleData => {
  return createPostRequest({
    api: apis.articleSave,
    data: articleData,
  }).send()
}


const getCommentPage = (paras = {
  articleId,
  page
}) => {
  return createGetRequest({
    api: apis.commentPaginate,
    paras: paras
  }).send()
}
const getArticleLikes = (paras = {
  articleId
}) => {
  return createGetRequest({
    api: apis.getArticleLikes ,
    paras: paras
  }).send()
}
const getWinxinQrcodeImg = (paras = {
  articleId,
    path,
  openId
}) => {
  return createGetRequest({
    api: apis.getWinxinQrcodeImg ,
    paras: paras
  }).send()
}

const toUnifiedOrder = (paras = {
  articleId,
  path,
  openId
}) => {
  return createGetRequest({
    api: apis.toUnifiedOrder ,
    paras: paras
  }).send()
}

/**
 * 发布评论
 */
const doPostComment = (paras = {
  articleId,
  pid
},content) => {
  return createPostRequest({
    api: apis.postComment,
    paras: paras,
    data: content,
  }).send()
}

///////////////////////article api end/////////////////////////////////


///////////////////////page api start/////////////////////////////////

/**
 * 获取页面信息
 */
const getPage = id => {
  return createGetRequest({
    api: apis.pageInfo,
    paras: { id: id }
  }).send()
}

/**
 * 获取页面列表
 */
const getPageList = flag => {
  return createGetRequest({
    api: apis.pageList,
    paras: { flag: flag }
  }).send()
}

///////////////////////page api end/////////////////////////////////


const isLogined = () => {
  return config.jwt != "";
}




module.exports = {
    config: config,
    getUrl:getUrl,
    init: init, //初始化
    createGetRequest: createGetRequest, //构建一个Get API请求
    createPostRequest: createPostRequest, //构建一个Post API请求
    createRequest: createRequest, //构建一个API请求，默认是get请求
    wxLogin: code2session, //进行用户code初始化
    wxGetUserInfo: decryptUserInfo, //进行用户注册 或 初始化当前用户信息
    isLogined: isLogined,

    // 配置相关 //
    getOption: getOption,
    getAppName: getAppName,
    getCopyright: getCopyright,
    getSlides: getSlides,
    getEnableComment: getEnableComment,


    // 用户相关 //
    getUser: getUser,
    getMyInfo: getMyInfo,
    doUserSave: doUserSave,
    getMyCommentArticles :getMyCommentArticles,
    getMyLikeArticles :getMyLikeArticles,
    getMyPraisePosts :getMyPraisePosts,
    getMySubPost :getMySubPost,
    getNewComments :getNewComments,
    likeArticle:likeArticle,
    getMySubscribes: getMySubscribes,
    toSubscribe: toSubscribe,

    // 文章相关 //
    getArticle: getArticle,
    getArticleBySlug: getArticleBySlug,
    getArticleList: getArticleList,
    getArticleRelevantList:getArticleRelevantList,
    getArticlePage: getArticlePage,
    getArticleCategory: getArticleCategory,
    getArticleCategories: getArticleCategories,
    doArticleSave: doArticleSave,
    doPostComment: doPostComment,
    getCommentPage: getCommentPage,
    getArticleLikes : getArticleLikes ,
    getWinxinQrcodeImg: getWinxinQrcodeImg,

    // 支付
    toUnifiedOrder: toUnifiedOrder,

    // 页面相关 //
    getPage: getPage,
    getPageList: getPageList,
}
